<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018-09-03
 * Time: 11:12
 * table user position
 * 系统权限操作
 */

namespace app\index\controller;


class Jurisdiction
{
    //获取职位
    public static function getPositionsDb()
    {
        if(!empty($perssion = perssion([LOGIN])))   //权限判断
            return $perssion;

        $allMajor = EquipRecord::getData(['parentId'=>User::getStoreId()], false); //获取所有专业

        foreach ($allMajor as $key=>$major) {
            renameArray($allMajor[$key], 'name', 'position');   //职位
        }
        array_push($allMajor, ['id'=>'1', 'position'=>'经理']);
        array_push($allMajor, ['id'=>'0', 'position'=>'其他']);

        return RD(SUCCESS, $allMajor);
    }
    /**
     * 设置权限
     * @param string $peopleId    用户id
     * @param string $rankName    权限级别名称
     * @return array
     */
    public static function setJurisdictionDb($peopleId = null, $rankName = [])
    {
        if(!empty($perssion = perssion([LOGIN])))   //权限判断
            return $perssion;

        $peopleId   = input('peopleId');
        $rankName = input('rankName');

        if(empty($peopleId))
            return RD(PARA_NOT_NULL);

        $user = User::getData(['id'=>$peopleId, 'rankName'=>$rankName]);
        if(empty($user)) {
            User::setData(['id'=>$peopleId], ['rankName'=>$rankName, 'client_id'=>'']);
        }
        return RD(SUCCESS);
    }
    /**
     * 获取所有职位权限
     * @return array
     */
    public static function getJurisdictionDb()
    {
        $data = [
            ['rankName'=>"经理", 'Jurisdiction'=>['分配权限', '分配人员', '门店管理', '报表管理', '编辑档案/计划/作业单/培训/能耗', '考核评分','报修', '发起考试']],
            ['rankName'=>"主任", 'Jurisdiction'=>['门店管理', '报表管理', '编辑档案/计划/作业单/培训/能耗', '考核评分','维修', '报修', '发起考试']],
            ['rankName'=>"员工", 'Jurisdiction'=>['维修', '报修']],
            ['rankName'=>"商户", 'Jurisdiction'=>['报修']],
            ['rankName'=>"保安", 'Jurisdiction'=>['报修']],
            ['rankName'=>"保洁", 'Jurisdiction'=>['报修']],
        ];
        return RD(SUCCESS, $data);
    }
    /**
     * 获取人员分配
     * @param string $storeId 门店id
     * @return array
     */
    public static function getPositionsByStoreDb($storeId = null)
    {
        if(!empty($perssion = perssion([LOGIN])))   //权限判断
            return $perssion;

        if(empty($storeId)) {
            $storeId = input('storeId');
            $storeId = !empty($storeId) ? $storeId : User::getStoreId();
        }
        $positionList = self::getPositionsDb()['data'];
        $data = [];
        foreach ($positionList as $position)
        {
            $peoples = [];
            //获取所有用户ID client_id,   weixin_id, username, passwd, phone, email, position_id, store_id, img
            $result =  User::getData(['position_id'=>$position['id'], 'store_id'=>$storeId], false);

            foreach ($result as $value)
            {
                array_push($peoples, [
                    'name' => $value['username'],
                    'id'   => $value['id'],
                    'phone'=> $value['phone'],
                    'rankName'=> $value['rankName'],
                ]);
            }
            array_push($data, ['id'=>$position['id'], 'position'=>$position['position'], 'peoples'=>$peoples]);
        }
        return RD(SUCCESS, $data);
    }
    /**
     * 设置门店人员分配
     * @param string $storeId      门店id
     * @param string $positionId   职位id
     * @param array $peoplesId     人员id[]
     * @return array
     */
    public static function allotPeoplesDb($storeId = null, $positionId = null, $peoplesId = null)
    {
        if(!empty($perssion = perssion([LOGIN])))   //权限判断
            return $perssion;

        $storeId    = input('storeId');
        $positionId = input('positionId');
        $peoplesId  = input('peoplesId/a');

        if(empty($storeId) || empty($peoplesId))
            return RD(PARA_NOT_NULL);

        foreach ($peoplesId as $popleId)
        {
            $result = User::getData(['id'=>$popleId]);
            $updateData = [];
            if($result['position_id'] != $positionId) {
                $updateData['client_id']   = '';   //退出该id登录
                $updateData['position_id'] = $positionId;
            }
            if($result['store_id'] != $storeId) {
                cache($popleId.'storeId', NULL);         //清空缓存
                $updateData['store_id'] = $storeId;
            }
            if(!empty($updateData)) {
                User::setData(['id'=>$popleId], $updateData);
            }
        }
        return RD(SUCCESS);
    }
}